package com.chinasoft.goodsManager.servlet;

import com.alibaba.fastjson.JSON;
import com.chinasoft.goodsManager.domain.Goods;
import com.chinasoft.goodsManager.service.LoginService;
import com.chinasoft.goodsManager.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/queryGoodsServlet")
public class QueryGoodsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LoginService loginService = new LoginServiceImpl();

        //设置请求字符集编码，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        Integer id = Integer.valueOf(request.getParameter("searchid"));
        Goods goods = loginService.findGoodsById(id);
        //读取详细信息文本
        String descrike = "";
        InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\GoodManagerFile\\GoodsFile\\"+goods.getGdescrike()),"UTF-8");
        int read;
        while ((read = isr.read())!=-1){
            descrike = descrike + (char)read;
        }
        isr.close();
        goods.setGdescrike(descrike);
        if (goods!=null){
            String str = JSON.toJSONString(goods);
            response.getWriter().write(str);
        }else {
            response.getWriter().write("0");
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
